package com.yangchoi.jetpackcompose.ui.baseui

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.yangchoi.jetpackcompose.GlobalViewModel
import com.yangchoi.jetpackcompose.LocalGlobalViewModel
import com.yangchoi.jetpackcompose.route.BottomTabBarRoute
import com.yangchoi.jetpackcompose.route.RouteKey
import com.yangchoi.jetpackcompose.route.RouteNavigationUtil
import com.yangchoi.jetpackcompose.theme.grey3
import com.yangchoi.jetpackcompose.theme.white

/**
 * 底部导航栏
 * */
@Composable
fun BottomTabBar(navHostController: NavHostController,selectIndex:Int,onItemSelected:(position:Int)->Unit) {
    val globalCurrent = LocalGlobalViewModel.current
    val tabBarList = listOf(
        BottomTabBarRoute.Home,
        BottomTabBarRoute.Project,
        BottomTabBarRoute.Navi,
        BottomTabBarRoute.Mine
    )

    BottomNavigation(
        modifier = Modifier.height(45.dp)
    ) {
        val navBackStackEntry by navHostController.currentBackStackEntryAsState()
        val currentDestination = navBackStackEntry?.destination
        tabBarList.forEachIndexed { index,screen ->
            BottomNavigationItem(
                modifier = Modifier
                    .background(MaterialTheme.colors.primary)
                    .offset(y = 5.dp),
                icon = {
                    var iconRes = painterResource(id = screen.iconUnSelected)
                    if(selectIndex == index){
                        iconRes = painterResource(id = screen.iconSelected)
                    }
                    Image(painter = iconRes,
                        contentDescription = null,
                        modifier = Modifier.height(20.dp),
                    )
                },
                label = {
                    var textColor = grey3
                    if (selectIndex == index)
                        textColor = white
                    Text(text = stringResource(screen.stringId),
                        fontSize = 12.sp,
                        color = textColor
                    )
                },
                selected = currentDestination?.hierarchy?.any {
                    it.route == screen.key.toString()
                } == true,
                onClick = {
                    if (index == 3){
                        if (globalCurrent.globalState.isUserLogin){
                            onItemSelected.invoke(index)
                            if (currentDestination?.route != screen.key.toString()) {
                                navHostController.navigate(screen.key.toString()) {
                                    popUpTo(navHostController.graph.findStartDestination().id) {
                                        saveState = true
                                    }
                                    launchSingleTop = true
                                    restoreState = true
                                }
                            }
                        }else{
                            RouteNavigationUtil.navigationTo(navCtrl = navHostController, destinationName = RouteKey.LOGIN.toString())
                        }

                    }else{
                        onItemSelected.invoke(index)
                        if (currentDestination?.route != screen.key.toString()) {
                            navHostController.navigate(screen.key.toString()) {
                                popUpTo(navHostController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                        }
                    }
                })
        }
    }

}